<% @billboards = get_homepage_sidebar_billboards %>
<aside class="side-bar sidebar-additional showing grid gap-4" id="sidebar-additional">
  <% if @billboards.first %>
    <%= render partial: "shared/billboard", locals: { billboard: @billboards.first, data_context_type: BillboardEvent::CONTEXT_TYPE_HOME } %>
  <% end %>
  <% if user_signed_in? && @active_discussions.any? %>
    <% if Settings::General.display_sidebar_active_discussions %>
      <section class="crayons-card crayons-card--secondary crayons-layout__content" id="active-discussions">
        <header class="crayons-card__header">
          <h3 class="crayons-subtitle-2">
            <%= t("views.main.side.active_discussions") %>
          </h3>
        </header>
        <div>
          <% @active_discussions.each do |plucked_article| %>
            <% next if plucked_article[1] == "[Boost]" %>
            <%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: true %>
          <% end %>
        </div>
      </section>
    <% end %>
  <% end %>

  <% if @billboards.second %>
    <%= render partial: "shared/billboard", locals: { billboard: @billboards.second, data_context_type: BillboardEvent::CONTEXT_TYPE_HOME } %>
  <% end %>

  <% cache(release_adjusted_cache_key("main-article-right-sidebar-discussions-#{params[:timeframe]}-#{user_signed_in?}-#{RequestStore.store[:subforem_id]}"), expires_in: (params[:timeframe].blank? ? 90 : 180).seconds) do %>
    <% if is_root_subforem? %>
      <% Subforem.where(discoverable: true, root: false).each do |subforem| %>
        <% order = Arel.sql("last_comment_at + (INTERVAL '1 minute' * LEAST(comment_score, 50)) DESC") %>
        <% articles = Article.where(subforem_id: subforem.id).published
          .where("comments_count > ?", 0)
          .with_at_least_home_feed_minimum_score
          .where("published_at > ?", 1.week.ago)
          .limit(5)
          .order(order)
          .pluck(:path, :title, :comments_count, :created_at, :subforem_id) %>
        <% next if articles.empty? %>
        <section class="crayons-card crayons-card--secondary crayons-layout__content">
          <header class="crayons-card__header">
            <h3 class="crayons-subtitle-2">
              <a href="https://<%= subforem.domain %>" class="crayons-link">
                <img src="<%= Settings::General.logo_png(subforem_id: subforem.id) %>" alt="<%= Settings::Community.community_name(subforem_id: subforem.id) %>" class="crayons-avatar crayons-avatar--s mr-1" style="border-radius: 3px !important" />
                <%= Settings::Community.community_name(subforem_id: subforem.id) %>
              </a>
            </h3>
            <div class="fs-xs color-base-70">
              <%= Settings::Community.community_description(subforem_id: subforem.id) %>
            </div>
          </header>
          <div>
            <% articles.each do |plucked_article| %>
              <% next if plucked_article[1] == "[Boost]" %>
              <%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: true %>
            <% end %>
          </div>
        </section>
      <% end %>
    <% else %>
      <% Tag.where(name: Settings::General.sidebar_tags).find_each do |tag| %>
        <section class="crayons-card crayons-card--secondary crayons-layout__content">
          <header class="crayons-card__header">
            <h3 class="crayons-subtitle-2"><a href="/t/<%= tag.name %>" class="crayons-link">#<%= tag.name %></a></h3>
            <div class="fs-xs color-base-70">
              <%= tag.short_summary %>
            </div>
          </header>
          <div>
            <% if tag.name == "help" %>
              <% Article.active_help.limit(5).pluck(:path, :title, :comments_count, :created_at, :subforem_id).each do |plucked_article| %>
                <% next if plucked_article[1] == "[Boost]" %>
                <%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: true %>
              <% end %>
            <% else %>
              <% active_threads(tags: [tag.name], time_ago: Timeframe.datetime(params[:timeframe]), count: 5).each do |plucked_article| %>
                <% next if plucked_article[1] == "[Boost]" %>
                <%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: true %>
              <% end %>
            <% end %>
          </div>
        </section>
      <% end %>
    <% end %>
  <% end %>
  <% if !user_signed_in? && RequestStore.store[:subforem_id] == RequestStore.store[:default_subforem_id] %>
    <% cache("seo-boostable-posts-homepage-#{params[:timeframe]}-#{RequestStore.store[:subforem_id]}", expires_in: 18.hours) do %>
      <% boostable_posts = Article.from_subforem.seo_boostable(nil, Timeframe.datetime(params[:timeframe])) %>
      <% if boostable_posts.present? %>
        <section class="widget">
          <header>
            <h4><%= t("views.main.side.trend") %></h4>
          </header>
          <div class="widget-body">
            <div class="widget-link-list">
              <% boostable_posts.each do |plucked_article| %>
                <% next if plucked_article[1] == "[Boost]" %>
                <%= render "articles/widget_list_item", plucked_article: plucked_article, show_comment_count: false %>
              <% end %>
            </div>
          </div>
        </section>
      <% end %>
    <% end %>
  <% end %>
  
  <% if @billboards.third %>
    <div class="sidebar-sticky-element" id="sticky-billboard">
      <%= render partial: "shared/billboard", locals: { billboard: @billboards.third, data_context_type: BillboardEvent::CONTEXT_TYPE_HOME } %>
    </div>
  <% end %>

</aside>
